package com.it.myd.schedule;


import com.it.myd.bean.box.world.Material;
import com.it.myd.bean.box.world.Resource;
import com.it.myd.help.ResourceLoader;
import com.it.myd.service.main.module.world.ItemService;
import com.it.myd.service.main.module.world.MaterialService;
import com.it.myd.service.main.module.world.PlayerService;
import com.it.myd.service.main.module.world.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableScheduling
@Slf4j
public class WorldSchedule {

    @Autowired
    private ResourceService resourceService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private PlayerService playerService;
    @Autowired
    private ItemService itemService;

    @Scheduled(fixedRate = 450L)
    public void RefreshResource() {
        if(playerService.getPlayerList().size() == 0){
            return;
        }
        playerService.playerMove();
        playerService.playerCreate();
        List<Resource> reslist = resourceService.getReslist();
        List<Resource> reslist_delete = new ArrayList<Resource>();
        if (reslist != null && reslist.size() > 0) {
            for (Resource resource : reslist) {
                Boolean flag = false;
                String o_output = resource.getRes_Output();
                if (resource.getRes_State().equals("2") && resource.getUseUser() != null) {
                    int user_sh = playerService.getPlayerSh(resource.getUseUser(),resource);
                    int o_hp = Integer.parseInt(resource.getRes_Hp());
                    int sh = Integer.parseInt(resource.getRes_Rec()) - user_sh;
                    int hp = Integer.parseInt(resource.getRes_Hp()) + sh;
                    if (hp > Integer.parseInt(resource.getRes_MaxHp())) {
                        resource.setRes_Hp(resource.getRes_MaxHp());
                    } else if (hp <= 0) {
                        resource.setRes_Hp("0");
                        resource.setRes_State("3");
                        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");
                        String overtime = sdf.format(System.currentTimeMillis());
                        resource.setOvertime(overtime);
                        flag = true;
                        reslist_delete.add(resource);
                        playerService.outPlayer(resource);
                    } else {
                        resource.setRes_Hp(String.valueOf(hp));
                    }
                    int n_hp = Integer.parseInt(resource.getRes_Hp());
                    if (n_hp < o_hp && n_hp <= 0) {
                        int out = Integer.parseInt(resource.getRes_Output()) + (o_hp - n_hp);
                        resource.setRes_Output(String.valueOf(out));
                    } else {
                        int out = Integer.parseInt(resource.getRes_Output()) + user_sh;
                        resource.setRes_Output(String.valueOf(out));
                    }
                } else if (resource.getRes_State().equals("1")) {
                    int hp = Integer.parseInt(resource.getRes_Hp()) + Integer.parseInt(resource.getRes_Rec());
                    if (hp > Integer.parseInt(resource.getRes_MaxHp())) {
                        resource.setRes_Hp(resource.getRes_MaxHp());
                    } else if (hp <= 0) {
                        resource.setRes_Hp("0");
                        resource.setRes_State("3");
                        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");
                        String overtime = sdf.format(System.currentTimeMillis());
                        resource.setOvertime(overtime);
                        flag = true;
                        reslist_delete.add(resource);
                        playerService.outPlayer(resource);
                    } else {
                        resource.setRes_Hp(String.valueOf(hp));
                    }
                }
                if (flag) {
                    resourceService.updateResource(resource);
                }
                String n_output = resource.getRes_Output();
                List<Material> list = resource.getMaterial(o_output,n_output);
                if(list.size() > 0){
                    for (Material material: list) {
                        if(material.getOwnUser()!= null){
                            materialService.addMaterial(material.getOwnUser(),material);
                        }
                    }
                }
                if(flag){
                    List<Material> deadlist = resource.dead();
                    if(deadlist != null && deadlist.size() > 0){
                        for (Material material: list) {
                            if(material.getOwnUser()!= null){
                                materialService.addMaterial(material.getOwnUser(),material);
                            }
                        }
                    }
                }
            }
        }else{
            reslist = new ArrayList<Resource>();
            ResourceLoader resLoader = new ResourceLoader();
            reslist.add(resLoader.GatRes_Tree());
            reslist.add(resLoader.GatRes_Tree());
            reslist.add(resLoader.GatRes_Rock());
            reslist.add(resLoader.GatRes_Rock());
            for (Resource resource: reslist) {
                resourceService.addResource(resource);
            }
            resourceService.setReslist(reslist);
        }
        for(Resource resource : reslist_delete) {
            reslist.remove(resource);
        }
        resourceService.setReslist(reslist);
    }
}
